package com.dst.smart.handler.disposer;

import org.json.JSONArray;
import org.json.JSONObject;
import com.dst.smart.handler.entity.SemanticResult;
import com.dst.smart.handler.entity.ServiceType;
import com.dst.smart.music.MusicPlayMedia;
import com.dst.smart.player.InsType;
import com.dst.smart.player.entity.SongPlayInfo;
import android.content.Context;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
/**
 *	收音机
 * 	听收音机
 	我想听安徽交通广播电台
 	我想听fm90.5
 *
 * author <a href="http://www.xfyun.cn">讯飞开放平台</a>
 * date 2016年9月5日 下午4:58:10
 *
 */
public class RadioDisposer extends Disposer {

	public RadioDisposer(Context context) {
		super(context);
	}

	@Override
	public void disposeResult(SemanticResult semanticResult) {

		try {
			if (semanticResult != null) {
				JSONArray result = semanticResult.getResult();
				if (result != null && result.length() != 0) {
					final List<SongPlayInfo> playList = new ArrayList<SongPlayInfo>();
					for (int i = 0; i < result.length(); i++) {
						JSONObject newsJson = result.getJSONObject(i);
						String url = newsJson.getString("url");
						String description = newsJson.getString("description");
						SongPlayInfo playInfo = new SongPlayInfo();
						playInfo.setAnswerText(description);
						playInfo.setPlayUrl(url);
						playList.add(playInfo);
					}
					if (MusicPlayMedia.getInstansMedia() == null) {
						new MusicPlayMedia(mContext, "");
					}
					MusicPlayMedia.getInstansMedia().playURLList(playList);
					MusicPlayMedia.getInstansMedia().setFileName(1,
							playList.get(0).getPlayUrl(), 1);
					MusicPlayMedia.getInstansMedia().play();
					if (semanticResult.getAnswerText().length() > 0) {
						//先播放標題
						getPlayController().playURLList(semanticResult.getUUID(), null);
						getPlayController().playText(semanticResult.getUUID(), semanticResult.getAnswerText(), true, "", null);
					}
					sendAiuiData("收音机", "radio", result.toString());
				} else {
					if (semanticResult.getAnswerText().length() > 0) {
						//MainActivity.playSound(4,semanticResult.getAnswerText());
						getPlayController().playURLList(semanticResult.getUUID(), null);
						getPlayController().playText(semanticResult.getUUID(), semanticResult.getAnswerText(), true, "", null);
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public boolean canHandle(ServiceType type){
		return (type == ServiceType.RADIO);
	}
	

	private void parseRadioCommand(JSONObject semantic, String uuid) {
		try {
			String insType = semantic.getJSONObject(KEY_SLOTS).getString(KEY_INSTYPE);
			
			//到音乐控制指令的转换
			if(insType.equalsIgnoreCase("CLOSE") | insType.equalsIgnoreCase("PAUSE") ){
				insType = "pause";
			}
			
			if(insType.equalsIgnoreCase("PLAY")){
				insType = "replay";
			}
			
			getPlayController().onMusicCommand(uuid, InsType.parseInsType(insType));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
